/**
 * APP 公共的方法
 */
// import { Dialog, Toast, Notify } from 'vant'
// import dayjs from 'dayjs'
// import { Toast } from 'vant'
// import { Notify } from 'vant'
export default {
  
  /**
   * 获取系统当前时间  年月日时分秒
   */
  GetData() {
    let date = new Date();
    let seperator1 = "-";
    let seperator2 = ":";
    let month = date.getMonth() + 1;
    let strDate = date.getDate();
    let hour = date.getHours();
    let minute = date.getMinutes();
    let second = date.getSeconds();
    if (month >= 1 && month <= 9) {
      month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
      strDate = "0" + strDate;
    }
    if (hour < 10) {
      hour = "0" + hour;
    }
    if (minute < 10) {
      minute = "0" + minute;
    }
    if (second < 10) {
      second = "0" + second;
    }
    let currentdate =
      String(date.getFullYear()) +
      String(seperator1) +
      String(month) +
      String(seperator1) +
      String(strDate) +
      " " +
      String(hour) +
      String(seperator2) +
      String(minute) +
      String(seperator2) +
      String(second);
    return currentdate;
  },
  getWeekDate() {
    let now = new Date(),
      day = now.getDay(),
      weeks = new Array(
        "星期日",
        "星期一",
        "星期二",
        "星期三",
        "星期四",
        "星期五",
        "星期六"
      );
    var week = weeks[day];
    return week;
  },
 
  /**
   *
   * @param {传入时间格式为yyyyMMddhhmmss 返回格式化时间为yyyy-MM-DD HH:mm:ss格式} time
   */
  filterTime(time, Division = "-") {
    if (time == undefined || time == null) {
      return null;
    }
    return (
      time.slice(0, 4) +
      `${Division}` +
      time.slice(4, 6) +
      `${Division}` +
      time.slice(6, 8) +
      " " +
      time.slice(8, 10) +
      ":" +
      time.slice(10, 12) +
      ":" +
      time.slice(12, 14)
    );
  },
  /**
   *
   * 格式化element日期和时间范围 格式化时间为yyyy-MM-DD HH:mm:ss格式
   * @param {传入的中国标准时间} d
   */
  parseTime(d) {
    let getMonth = d.getMonth() + 1;
    let getDate = d.getDate();
    let getHours = d.getHours();
    let getMinutes = d.getMinutes();
    let getSeconds = d.getSeconds();
    if (d.getMonth() + 1 >= 0 && d.getMonth() + 1 <= 9) {
      getMonth = "0" + getMonth;
    }
    if (getDate >= 0 && getDate <= 9) {
      getDate = "0" + getDate;
    }
    if (getHours >= 0 && getHours <= 9) {
      getHours = "0" + getHours;
    }
    if (getMinutes >= 0 && getMinutes <= 9) {
      getMinutes = "0" + getMinutes;
    }
    if (getSeconds >= 0 && getSeconds <= 9) {
      getSeconds = "0" + getSeconds;
    }
    const newDate =
      d.getFullYear() +
      "-" +
      getMonth +
      "-" +
      getDate +
      " " +
      getHours +
      ":" +
      getMinutes +
      ":" +
      getSeconds;
    return newDate;
  },
    /**
   * 对象去重
   */
  deteleObject(obj) {
    var uniques = [];
    var stringify = {};
    for (var i = 0; i < obj.length; i++) {
      var keys = Object.keys(obj[i]);
      keys.sort(function (a, b) {
        return Number(a) - Number(b);
      });
      var str = "";
      for (var j = 0; j < keys.length; j++) {
        str += JSON.stringify(keys[j]);
        str += JSON.stringify(obj[i][keys[j]]);
      }
      if (!stringify.hasOwnProperty(str)) {
        uniques.push(obj[i]);
        stringify[str] = true;
      }
    }
    // uniques = uniques
    return uniques;
  },
  /**
   *
   * @param {传入事件格式} data
   */
  splitdata(data) {
    let gettime = data.split("/").join("");
    let mydatatime = gettime + "000000";
    return mydatatime;
  },
  // 获取本周1-7的年月日
  WeekTime() {
    var newDate = new Date();
    var timesStamp = newDate.getTime(); // 时间戳
    var currenDay = newDate.getDay();
    var dates = [];
    for (var i = 0; i < 7; i++) {
      dates.push(
        new Date(timesStamp + 24 * 60 * 60 * 1000 * (i - ((currenDay + 6) % 7)))
          .toLocaleDateString()
          .replace(/[年月]/g, "-")
          .replace(/[日上下午]/g, "")
      );
    }
    return dates;
  },

  // 用于排行
  /**
   * 获取本周一 的时间
   *  */
  Monday() {
    var now = new Date();
    var nowTime = now.getTime();
    var day = now.getDay();
    var oneDayTime = 24 * 60 * 60 * 1000;
    var MondayTime = nowTime - (day - 1) * oneDayTime; // 显示周一
    // var SundayTime =  nowTime + (7-day)*oneDayTime ;//显示周日
    function p(s) {
      return s < 10 ? "0" + s : s;
    }
    const d = new Date(MondayTime);
    const begintime =
      String(d.getFullYear()) +
      String(p(d.getMonth() + 1)) +
      String(p(d.getDate())) +
      "000000";
    return begintime;
  },
  /**
   * 获取本周日 的时间
   *  */
  Sunday() {
    // 本周日的日期
    var now = new Date();
    var nowTime = now.getTime();
    var day = now.getDay();
    var oneDayTime = 24 * 60 * 60 * 1000;
    // var MondayTime = nowTime - (day - 1) * oneDayTime // 显示周一
    var SundayTime = nowTime + (7 - day) * oneDayTime; // 显示周日
    function p(s) {
      return s < 10 ? "0" + s : s;
    }
    const d = new Date(SundayTime);
    // const resTime = p(d.getHours()) + ':' + p(d.getMinutes()) + ':' + p(d.getSeconds());
    const resDate =
      String(d.getFullYear()) +
      String(p(d.getMonth() + 1)) +
      String(p(d.getDate())) +
      "235959"; // 当前周的周末
    return resDate;
  },
  /**
   * 获取本月的第一天
   */
  getCurrentMonthFirst() {
    var date = new Date();
    date.setDate(1);
    var month = String(date.getMonth() + 1);
    var day = date.getDate();
    if (month <= 9) {
      month = "0" + month;
    }
    if (day <= 9) {
      day = "0" + day;
    }
    return String(date.getFullYear()) + String(month) + String(day) + "000000";
  },
  /**
   * 获取本月的最后一天
   */
  getCurrentMonthLast() {
    var mydate = new Date();
    var currentMonth = mydate.getMonth();
    var nextMonth = ++currentMonth;
    var nextMonthFirstDay = new Date(mydate.getFullYear(), nextMonth, 1);
    var oneDay = 1000 * 60 * 60 * 24;
    var lastTime = new Date(nextMonthFirstDay - oneDay);
    var month = parseInt(lastTime.getMonth() + 1);
    var day = lastTime.getDate();
    if (month < 10) {
      month = "0" + month;
    }
    if (day < 10) {
      day = "0" + day;
    }
    var date = new Date(
      String(mydate.getFullYear()) + "-" + String(month) + "-" + String(day)
    );
    if (date.getMonth() + 1 <= 9) {
      var yue = "0" + (date.getMonth() + 1);
    } else {
      yue = date.getMonth() + 1;
    }
    let dateValue =
      String(date.getFullYear()) +
      String(yue) +
      String(date.getDate()) +
      "235959";
    return dateValue;
  },
  // 获取当前年月日
  getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
      month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
      strDate = "0" + strDate;
    }
    var currentdate =
      String(year) +
      String(seperator1) +
      String(month) +
      String(seperator1) +
      String(strDate);
    return currentdate;
  },
  /**
   *
   * @param {格式化中国标准时间} d
   * @param 返回yyyyMMddhhmmss
   *
   */

  timeFormatsss(d) {
    d = new Date(new Date(new Date().setHours(0, 0, 0, 0)));
    let getMonth = d.getMonth() + 1;
    let getDate = d.getDate();
    let getHours = d.getHours();
    let getMinutes = d.getMinutes();
    let getSeconds = d.getSeconds();
    if (d.getMonth() + 1 >= 0 && d.getMonth() + 1 <= 9) {
      getMonth = "0" + getMonth;
    }
    if (getDate >= 0 && getDate <= 9) {
      getDate = "0" + getDate;
    }
    if (getHours >= 0 && getHours <= 9) {
      getHours = "0" + getHours;
    }
    if (getMinutes >= 0 && getMinutes <= 9) {
      getMinutes = "0" + getMinutes;
    }
    if (getSeconds >= 0 && getSeconds <= 9) {
      getSeconds = "0" + getSeconds;
    }
    const newDate =
      String(d.getFullYear()) +
      String(getMonth) +
      String(getDate) +
      String(getHours) +
      String(getMinutes) +
      String(getSeconds);
    return newDate;
  },
  doHandleMonth: function (month) {
    var m = month;
    if (month.toString().length == 1) {
      m = "0" + month;
    }
    return m;
  },
  CurentTime: function () {
    let now = new Date();
    let year = now.getFullYear(); // 年
    let month = now.getMonth() + 1; // 月
    let day = now.getDate(); // 日
    var clock = year + "年";
    if (month < 10) {
      clock += "0";
    }
    clock += month + "月";
    if (day < 10) {
      clock += "0";
    }
    clock += day + "日";
    return clock;
  },
  theCurrentTime() {
    let myDate = new Date();
    let h =
      myDate.getHours() < 10 ? "0" + myDate.getHours() : myDate.getHours();
    let m =
      myDate.getMinutes() < 10
        ? "0" + myDate.getMinutes()
        : myDate.getMinutes();
    let s =
      myDate.getSeconds() < 10
        ? "0" + myDate.getSeconds()
        : myDate.getSeconds();
    return h + ":" + m + ":" + s;
  },
  getDay: function (day) {
    var today = new Date();
    var targetdayMilliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
    today.setTime(targetdayMilliseconds); // 注意，这行是关键代码
    // var tYear = today.getFullYear();
    var tMonth = today.getMonth();
    var tDate = today.getDate();
    tMonth = this.doHandleMonth(tMonth + 1);
    tDate = this.doHandleMonth(tDate);
    return tMonth + "." + tDate;
  },
  /**
   * GPS经纬度转化成高德坐标
   * @param {数组[106.55635, 29.538654]} gps
   */
  positionSwitch(gps) {
    new window.AMap.convertFrom(gps, "gps", function (status, result) {
      if (result.info === "ok") {
        var lnglats = result.locations; // Array.<LngLat>
        console.log(lnglats);
      }
    });
  },
  /**
   * 计算经纬度差值
   * @param {我当前经度} lat1
   * @param {我当前纬度} lng1
   * @param {企业的经度} lat2
   * @param {企业的纬度} lng2
   */
  positionToCalculate(lat1, lng1, lat2, lng2) {
    function Rad(d) {
      return (d * Math.PI) / 180.0; // 经纬度转换成三角函数中度分表形式。
    }
    let radLat1 = Rad(lat1);
    let radLat2 = Rad(lat2);
    let a = radLat1 - radLat2;
    let b = Rad(lng1) - Rad(lng2);
    let s =
      2 *
      Math.asin(
        Math.sqrt(
          Math.pow(Math.sin(a / 2), 2) +
            Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)
        )
      );
    s = s * 6378.137; // EARTH_RADIUS;
    s = Math.round(s * 1000) / 1000; // 这里输出米   //输出为公里为 1Math.round(s * 10000) / 10000;
    // s = s.toFixed(4);
    let result = s.toString().split(".");
    // 位置完全相同
    if (result == 0) {
      return result[0];
    } else if (result[0] != 0) {
      // 当前距离大于一公里
      return null;
    }
    return result[1]; // 返回当前距离的米
  },

  /**
   * 提取字符串中的数字
   * @param {string} str
   */
  StringTakeInt(str = null) {
    return parseInt(str && String(str).replace(/[^0-9]/gi, ""));
  },
  currentTime() {
  	let date = new Date();
  	let seperator1 = "-";
  	let seperator2 = ":";
  	let month = date.getMonth() + 1;
  	let strDate = date.getDate();
  	let hour = date.getHours();
  	let minute = date.getMinutes();
  	let second = date.getSeconds();
  	if (month >= 1 && month <= 9) {
  		month = "0" + month
  	}
  	if (strDate >= 0 && strDate <= 9) {
  		strDate = "0" + strDate
  	}
  	if (hour < 10) {
  		hour = "0" + hour
  	}
  	if (minute < 10) {
  		minute = "0" + minute
  	}
  	if (second < 10) {
  		second = "0" + second
  	}
  	let currentdate = String(date.getFullYear()) + String(seperator1) + String(month) + String(seperator1) + String(
  		strDate) + " " + String(hour) + String(seperator2) + String(minute) + String(seperator2) + String(second);
  	return currentdate
  },
  Storage(){
	  const Session = {
	  	isSession(Session) {
	  		return Session ? sessionStorage : localStorage
	  	},
	  	ISKET(key) {
	  		if (!key) {
	  			throw Error("请传入Session的KEY!!!");
	  		}
	  	},
	  	setSession(key, setObj, Session) {
	  		this.ISKET(key);
	  		if (Object.keys(setObj).length > 0) {
	  			setObj = JSON.stringify(setObj)
	  		}
	  		let set = encodeURIComponent(setObj),
	  			result = btoa(set),
	  			Storage = Session ? sessionStorage : localStorage;
	  		this.isSession(Session).setItem(key, result)
	  	},
	  	getSession(key, Session) {
	  		this.ISKET(key);
	  		let data = this.isSession(Session).getItem(key),
	  			jieMi = atob(data),
	  			jieM = decodeURIComponent(jieMi);
	  		if (data) {
	  			try {
	  				return JSON.parse(jieM)
	  			} catch (e) {
	  				console.error("getSession方法获取数据错误");
	  				return false
	  			}
	  		}
	  	},
	  	sessionClear(key, Session) {
	  		this.ISKET(key);
	  		this.isSession(Session).removeItem(key)
	  	},
	  	sessionClearAll(Session) {
	  		this.isSession(Session).clear()
	  	},
	  };
  },
  padLeftZero (str) {
    str = str + ''
    return ('00' + str).substr(str.length)
  },
   formatDate (timestamp, fmt) {
    var date = new Date(timestamp * 1000)
    var nowYear = new Date().getFullYear()
  
    // 如果需要年份，则格式化年份；如果不需要，则只有当前年没有年份
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    } else if (date.getFullYear() < nowYear) {
      fmt = date.getFullYear() + '-' + fmt
    }
  
    var o = {
      'M+': date.getMonth() + 1,
      'd+': date.getDate(),
      'h+': date.getHours(),
      'm+': date.getMinutes(),
      's+': date.getSeconds()
    }
  
    for (let k in o) {
      if (new RegExp(`(${k})`).test(fmt)) {
        let str = o[k] + ''
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : this.padLeftZero(str))
      }
    }
  
    return fmt
  },
  setHighlight (keyword, item, className) {
    let s = new Set()
    for (let k of keyword) {
      s.add(k)
    }
    s.forEach((value)=> {
      item = item.replace(value,  ()=> {
        return `<em class="${className}">${value}</em>`
      })
    })
    return item
  },
  
  decode(getData){
	  let jieMi = atob(getData),
	  	jieM = decodeURIComponent(jieMi);
	  try {
	  	return JSON.parse(jieM)
	  } catch (e) {
	  	return false
	  }
	  
  }
  
};
